package com.bossly.metro;

import java.util.ArrayList;
import java.util.Hashtable;

import com.bossly.metro.dom.Station;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Filter;
import android.widget.Filterable;
import android.widget.ImageView;
import android.widget.TextView;

public class StationsAdapter extends BaseAdapter implements Filterable {
	private int[] icons = new int[] { R.drawable.point_green,
			R.drawable.point_red, R.drawable.point_blue,
			R.drawable.point_yellow };

	private Hashtable<Integer, Integer> m_icons;
	private Context mContext;
	private ArrayList<Station> m_stations;
	private CoreApplication m_app;

	public StationsAdapter(Context context, CoreApplication app) {
		mContext = context;
		m_app = app;
		m_stations = m_app.getMetropoliten().getStations("*");

		updateHash();
	}

	public void refresh() {
		m_stations = m_app.getMetropoliten().getStations("*");
		updateHash();
		notifyDataSetChanged();
	}

	private void updateHash() {
		m_icons = new Hashtable<Integer, Integer>();
		Station c;
		int n = 0;

		for (int i = 0, length = getCount(); i < length; i++) {
			c = (Station) getItem(i);
			int line = c.getLine();

			if (!m_icons.containsKey(line))
				m_icons.put(line, icons[n++]);
		}
	}

	@Override
	public int getCount() {
		return m_stations != null ? m_stations.size() : 0;
	}

	@Override
	public Object getItem(int position) {
		return m_stations.get(position);
	}

	@Override
	public long getItemId(int position) {
		return m_stations.get(position).getId();
	}

	@Override
	public View getView(int arg0, View view, ViewGroup arg2) {
		if (view == null) {
			view = LayoutInflater.from(mContext).inflate(R.layout.metro_item,
					null);
		}

		Station station = (Station) getItem(arg0);

		int line = station.getLine();

		ImageView image = (ImageView) view.findViewById(android.R.id.icon1);
		image.setImageResource(m_icons.get(line));

		TextView txt1 = (TextView) view.findViewById(android.R.id.text1);
		txt1.setText(station.getName());

		TextView txt2 = (TextView) view.findViewById(android.R.id.text2);
		txt2.setText(station.getLineName());

		return view;
	}

	@Override
	public Filter getFilter() {
		// TODO Auto-generated method stub
		return new StationFilter(m_app.getMetropoliten());
	}

	public class StationFilter extends Filter {
		private Metropoliten mMetro;

		public StationFilter(Metropoliten metro) {
			mMetro = metro;
		}

		@Override
		protected FilterResults performFiltering(CharSequence constraint) {
			FilterResults results = new FilterResults();

			if (constraint != null) {
				ArrayList<Station> stations = mMetro.getStations(constraint
						.toString());

				results.count = stations.size();
				results.values = stations;
			}

			return results;
		}

		@SuppressWarnings("unchecked")
		@Override
		protected void publishResults(CharSequence constraint,
				FilterResults results) {
			if (results.values != null) {
				m_stations = (ArrayList<Station>) results.values;
			}

			notifyDataSetChanged();
		}

	}
}
